package oa.struts.action;

import java.util.Collection;

import javax.annotation.Resource;

import oa.domain.forum.Forum;
import oa.domain.forum.Topic;
import oa.domain.page.PageBean;
import oa.service.ForumService;
import oa.service.TopicService;
import oa.struts.action.base.BaseAction;
import oa.util.HQLHelper;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

@Controller("forumAction")
public class ForumAction extends BaseAction<Forum> {

	/**
	 * <option value="0">全部主题</option>
	 * <option value="1">全部精华贴</option>
	 */
	private int viewType;//属性驱动，显示哪些主题
	
	/**
	 *  <option value="0">默认排序（按最后更新时间排序，但所有置顶帖都在前面）</option>
		<option value="1">按最后更新时间排序</option>
		<option value="2">按主题发表时间排序</option>
		<option value="3">按回复数量排序</option>
	 */
	private int orderBy;//属性驱动，排序字段
	
	/**
	 * <option value="false">降序</option>
	   <option value="true">升序</option>
	 */
	private boolean asc;//属性驱动，升序或者降序
	
	@Resource(name="forumService")
	private ForumService forumService;
	
	@Resource(name="topicService")
	private TopicService topicService;
	
	public int getViewType() {
		return viewType;
	}

	public void setViewType(int viewType) {
		this.viewType = viewType;
	}

	public int getOrderBy() {
		return orderBy;
	}

	public void setOrderBy(int orderBy) {
		this.orderBy = orderBy;
	}

	public boolean isAsc() {
		return asc;
	}

	public void setAsc(boolean asc) {
		this.asc = asc;
	}

	public String showAllForum()
	{
//		Collection<Forum> forumList = this.forumService.getAllForum();
//		ActionContext.getContext().put("forumList", forumList);
		HQLHelper hh = new HQLHelper(Forum.class);
		hh.addOrderBy("o.position", true);
		
		PageBean pageBean = this.forumService.getPageBean(hh,currentPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return listAction;
	}
	
	public String forumShow()
	{
		Forum forum = this.forumService.getForumById(this.getModel().getId());
//		BeanUtils.copyProperties(this.getModel(), forum);
		ActionContext.getContext().getValueStack().push(forum);
		
//		Collection<Topic> topicList = this.topicService.findTopicByForum(forum);
//		ActionContext.getContext().put("topicList", topicList);
		
		HQLHelper hh = new HQLHelper(Topic.class);
		if(viewType == 1)
		{
			//查询精华帖
			hh.addWhere(" o.type = ? ", 1);
		}
		
		if(orderBy == 0)
		{
			//<option value="0">默认排序（按最后更新时间排序，但所有置顶帖都在前面）</option>
			hh.addOrderBy(" CASE o.type WHEN 2 THEN 2 ELSE 1 END ", false);
			hh.addOrderBy("o.lastUpdateTime", false);
		}
		else if(orderBy == 1)
		{
			//<option value="1">按最后更新时间排序</option>
			hh.addOrderBy("o.lastUpdateTime", asc);
		}
		else if(orderBy == 2)
		{
			//<option value="2">按主题发表时间排序</option>
			hh.addOrderBy("o.postTime", asc);
		}
		else
		{
			//<option value="3">按回复数量排序</option>
			hh.addOrderBy("o.replyCount", asc);
		}
		
		
		PageBean pageBean = this.forumService.getPageBean(hh, currentPage);
		ActionContext.getContext().getValueStack().push(pageBean);
		return "show";
	}
}
